﻿using Admin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Admin.Controllers
{
    public class WangLuoController : ApiController
    {
        // GET: api/WangLuo
        [HttpGet]
        public IHttpActionResult GetList(string StudentName, string FollowType, int page = 1, int limit = 5)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var datalist = db.Netfollows.Include("Users").AsEnumerable();
                if (!string.IsNullOrEmpty(StudentName))
                {
                    datalist = datalist.Where(p => p.StudentName.Contains(StudentName));
                }
                if (!string.IsNullOrEmpty(FollowType))
                {
                    datalist = datalist.Where(p => p.FollowType == FollowType);
                }
                var count = datalist.Count();
                var list = datalist.Select(p => new {
                    Id = p.Id,
                    StudentName = p.StudentName,
                    FollowTime = p.FollowTime.ToString(),
                    NextFollowTime = p.NextFollowTime.ToString(),
                    Content1 = p.Content1,
                    UserName = p.Users.LoginName,
                    FollowType = p.FollowType,
                    CreateTime = p.CreateTime,
                    FollowState = p.FollowState
                }).Skip((page - 1) * limit).Take(limit).ToList();
                return Json(new { count = count, data = list });
            }
        }
        [HttpGet]
        public IHttpActionResult GetList2(string Name, string age, int page = 1, int limit = 5, int UserId = 1)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list1 = db.Users.Select(p => new { Id = p.Id, LoginName = p.LoginName }).FirstOrDefault(p => p.Id == UserId);
                var datalist = db.Students.Include("Users").AsEnumerable();
                if (!string.IsNullOrEmpty(Name))
                {
                    datalist = datalist.Where(p => p.Name.Contains(Name));
                }
                if (!string.IsNullOrEmpty(age))
                {
                    datalist = datalist.Where(p => p.Age == Convert.ToInt32(age));
                }
                if (list1 != null)
                {
                    datalist = datalist.Where(p => p.CreateUser == list1.LoginName);
                }
                var count = datalist.Count();
                var list = datalist.Select(p => new {
                    Id = p.Id,
                    StudentId = p.Id,
                    Name = p.Name,
                    StudentName = p.Name,
                    Age = p.Age,
                    Sex = p.Sex,
                    Phone = p.Phone,
                    Weixin = p.Weixin,
                    StuStatus = p.StuStatus,
                    PerState = p.PerState,
                    AskerName = p.Users.LoginName,
                    AskerId = p.AskerId,
                    UserId = p.AskerId,
                    CreateUser = p.CreateUser,
                    QQ = p.QQ
                }).Skip((page - 1) * limit).Take(limit).ToList();
                return Json(new { count = count, data = list });
            }
        }
        [HttpPost]
        public IHttpActionResult Delete([FromBody] Netfollows net)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Netfollows.FirstOrDefault(p => p.Id == net.Id);
                db.Netfollows.Remove(list);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "删除成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "删除失败！" });
                }

            }
        }
        [HttpPost]
        public IHttpActionResult GetFollowTypeList()
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Netfollows.Select(p => p.FollowType).Distinct().ToList();
                return Json(list);
            }
        }
        [HttpPost]
        public IHttpActionResult GetFollowStateList()
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Netfollows.Select(p => p.FollowState).Distinct().ToList();
                return Json(list);
            }
        }
    }
}
